
#include "main.h" 
#include "Events.h" 
#include "printf.h" 

void InitDevice( void )
{
   CLRWDT();
	INTCON2 = 0;						  
	CMCON = 0x07;
   PORTA = 0;
   TRISA = 0b11111111;
   PORTB = 0;
	//TRISB = 0b10111111;
	TRISB = 0b10111111;
   PORTC = 0;
   TRISC = 0b11111111;
   PORTD = 0;
   TRISD = 0b11111111;
	PORTE = 0;

/* 	TRISE = 0b11111011; */

	PORTF = 0;
   TRISF = 0b11111111;
   PORTG = 0;
   TRISG = 0b11111111;  
   ADCON0 = 0;
   ADCON1 = 0x0E;               /* all inputs as digitital, RA0 - analog */
   CLRWDT();
// OPTION = 0b10001111;
   CLRWDT();
   PEIE = TRUE;
	/* System Clock Init */
	TMR0 = 0;
	T0CON = 0;
	T0CON = 0b10000010;
	TMR0IF = 0;
	//TMR0IE = TRUE;
	
	/* TMR4 as random generator */
	TMR4ON = TRUE;

/* 	I/O Init */
	
/* 	PIN_LCD_BACKLIGHT = TRUE;  */
/* 	PIN_LCD_BACKLIGHT_DIR = OUTPUT; */
/* 	PIN_KEYBOARD_BACKLIGHT = TRUE; */
/* 	PIN_KEYBOARD_BACKLIGHT_DIR = OUTPUT; */
	
	
}

  
void InitModules( void )
{
	PIN_MODEM_POWER = FALSE;
	PIN_MODEM_POWER_DIR = OUTPUT;

	
//	delay_ms(2000);

#ifdef   LCD_MODE
	lcd_init(20);
	lcd_cursor(OFF);
	lcd_print_str(STR__CELLNETS_COMPANY, 0, 1, 1);
	lcd_print_str(STR__SIMPHONY_VER_3, 0, 2, 0);
	//lcd_print_str(Revision_Basic, 0, 3, 0);
	GetRevision();
	lcd_print_str(Revision, 0,3,0);
#endif
	
	//Passwords_Init();

#ifdef   MODEM_TYPE__SIMCOM
 	PIN_MODEM_WAKEUP_DIR = OUTPUT;
	PIN_MODEM_WAKEUP = MODEM_SW_HIGH;
#endif
	
//	Ext_Display_Update();

	PIN_LED = 0;
	PIN_LED_DIR = OUTPUT;

//	PIN_POWER_CONTROL = TRUE;
//	PIN_POWER_CONTROL_DIR = OUTPUT;

	USART_Init();
	USART_2_Init();

	PIN_EXTERNAL_SIM = 1;
	PIN_EXTERNAL_SIM_DIR = OUTPUT;
	//delay_ms(500);
	
	PIN_SIM_ACTIVATE = TRUE; 
	PIN_SIM_ACTIVATE_DIR = OUTPUT;

	PIN_MODEM_POWER = TRUE;
	
	LED = 1;
	
	Config_Init();
//	IMSI_Range_Init();
//	AnalogInit();
	Ex_EEPROM_Init();

//	CDR_Init();
	Inputs_Init();
	Outputs_Init();

//	InputsHandlers_Init();
	No_Delete_SMS_For_User = FALSE;

   MainStateMachine_Init();
	Command_Init();
//   Sound_Init();
//	MenusInit();

	Init_RF_Receiver();
	Init_RF_Transmitter();
	Status_Init();
	
	INTCON2 |= 0x80; 				  /* all pull-ups are disable */

	RC1IE = TRUE; 
	RC2IE = TRUE;
	INTEDG0 = 0;
	//INT0IE = TRUE;
	INT0IE = FALSE;
	INT0IF = 0;

	delay_ms(1000);
	DebugOut_Enable = TRUE;
	
#if defined(APP_PRINT_SMS)
	putsf("\n\n\n\n === CELNETS COMPANY ===\n\r\n\r   Initialization\n please wait...\r", AUX_PORT);
	DebugOut_Enable = FALSE;
#elif defined(APP_CAR_PETROL_BLOCKING) || defined(APP_COMMAND_TRANSLATOR)
	putsf("\n\n\n\n === DIS Ltd ===\n\r\n\r   Initialization\n please wait...\r", AUX_PORT);
#endif
	delay_ms(1000);

	Event_Init();

	GIE = TRUE;

#ifdef   MODEM_TYPE__SIMCOM
	delay_ms(1000);
	PIN_MODEM_WAKEUP = MODEM_SW_LOW;
	delay_ms(2000);
	PIN_MODEM_WAKEUP = MODEM_SW_HIGH;
#endif

	Debug("\nEX_ADDR_FREE_SPACE = 0x%X", EX_ADDR_FREE_SPACE);
	/* 	if ( e2_Read(E2_ADDR_MANUFACTURE_TEST) ) */
/* 		SysState = MANUFACTURE_TEST; */
}

